/**
import { extend } from 'umi-request';
 * 获取数据类型
 * @param data
 * @returns
 * 0 => number
 * '' => string
 * {} => object
 * () => {} => function
 * [] => array
 * undefined => undefined
 * null => null
 * new Set() => set
 * new Map() => map
 * Symbol() => symbol
 */
export const getType = (data: any) => {
  const typeName = Object.prototype.toString.call(data).toLowerCase();
  return typeName.substring(8, typeName.length - 1);
};

/**
 * 是否包含带小数的浮点弄数值
 * @returns {boolean}
 */

/**
 * 获取小数位数
 * @param n 原始数值
 * @returns {number}
 */
const getFractionLength = (n: number) => {
  const fraction = n.toString().split('.')[1];
  return fraction ? fraction.length : 0;
};

/**
 * 是否包含带小数的浮点弄数值
 * @returns {boolean}
 */
function hasFraction(...args: any) {
  return args.some((item: any) => item >> 0 !== item);
}

// 除法，处理精度问题
export const hackDivision = (pn1 = 0, pn2 = 0) => {
  const n1 = pn1 === null || pn1 === undefined ? 0 : pn1;
  const n2 = pn2 === null || pn2 === undefined ? 0 : pn2;
  if (!n2) {
    return n1;
  }
  if (!hasFraction(n1, n2)) return n1 / n2;
  const l1 = getFractionLength(n1);
  const l2 = getFractionLength(n2);
  return (
    (Number(n1.toString().replace('.', '')) / Number(n2.toString().replace('.', ''))) *
    10 ** (l2 - l1)
  );
};

// 减法，处理精度问题
export const hackSub = (pn1 = 0, pn2 = 0) => {
  const n1 = pn1 === null || pn1 === undefined ? 0 : pn1;
  const n2 = pn2 === null || pn2 === undefined ? 0 : pn2;
  let r1;
  let r2;
  try {
    r1 = n1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = n2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  const m = 10 ** Math.max(r1, r2);
  const n = r1 >= r2 ? r1 : r2;
  return ((n1 * m - n2 * m) / m).toFixed(n);
};

/**
 * 递归格式化tree数据
 * @param treeItemList @type {{ [k in string]: string | T[] }}
 * @param config @type {Record<'title' | 'key' | 'children', keyof T>}  对应元数据的取值的key
 * @returns
 */
export const formatTreeData = <T extends { [k in string]: string | T[] }>(
  treeItemList: T[],
  config: Record<'title' | 'key' | 'children', keyof T> = {
    title: 'name',
    key: 'id',
    children: 'childResourcesList',
  },
): GLOBAL.TreeItem[] => {
  const loop = (list: T[]): GLOBAL.TreeItem[] => {
    if (!Array.isArray(list)) return [];
    return list.map((t: T): GLOBAL.TreeItem => {
      return {
        title: t[config.title] as string,
        key: t[config.key] as string,
        children: loop(t[config.children] as T[]),
      };
    });
  };
  const res = loop(treeItemList);
  if (res.length) {
    return res;
  }
  return [];
};

/*
 *数组对象去重
 */

export const removedSame = (arr: any, batch: string) => {
  if (!Array.isArray(arr)) {
    return arr;
  }
  if (arr.length == 0) {
    return [];
  }
  const obj = {};
  const uniqueArr = arr.reduce((total: any, item: any) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    obj[item[batch]] ? '' : (obj[item[batch]] = true && total.push(item));
    return total;
  }, []);
  return uniqueArr;
};

/**
 * 复制内容到剪贴板
 * @param value 需要复制的内容
 * @returns
 */
export function copyToBoard(value: string): boolean {
  const element = document.createElement('textarea');
  document.body.appendChild(element);
  element.value = value;
  element.select();
  if (document.execCommand('copy')) {
    document.execCommand('copy');
    document.body.removeChild(element);
    return true;
  }
  document.body.removeChild(element);
  return false;
}
